package com.prawn.ift.ctrlequipment.service.impl;


import com.prawn.ift.ctrlequipment.dao.CtrlequipmentDao;
import com.prawn.ift.ctrlequipment.dao.CtrlequipmentLogDao;
import com.prawn.ift.ctrlequipment.pojo.Ctrlequipment;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentChannel;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentLog;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentLogService;
import entity.Result;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import util.NullAttributeUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 控制设备操作记录管理服务实现
 */
@Service
public class CtrlEquipmentLogServiceImpl implements CtrlEquipmentLogService {

    @Autowired
    private CtrlequipmentLogDao ctrlequipmentLogDao;
    @Autowired
    private CtrlequipmentDao ctrlequipmentDao;

    /**
     * 添加控制设备操作记录
     *
     * @param ctrlEquipmentLog
     * @return Result
     */
    @Override
    public Result addCtrlEquipmentLog(CtrlequipmentLog ctrlEquipmentLog) {
        if (ctrlequipmentLogDao.save(ctrlEquipmentLog) != null) {
            //监听日志标志位的值是否为1 1--消息已经被消费 0--没有被处理
            long sleepTime = 500;
            while (true) {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sleepTime += 500;
                CtrlequipmentLog ctrlEquipmentLog1 = ctrlequipmentLogDao.findById(ctrlEquipmentLog.getId()).get();
                if (ctrlEquipmentLog1.getDelFlag().equals("1") && ctrlEquipmentLog1.getReturnResult().equals("1")) {
                    //修改控制设备状态
                    Ctrlequipment ctrlEquipment = new Ctrlequipment();
                    ctrlEquipment.setId(ctrlEquipmentLog.getCtrlEquipmentId());
                    ctrlEquipment.setUpdateDate(new Date());
                    if ("0x81".equals(ctrlEquipmentLog.getOperateType()) || "0x02".equals(ctrlEquipmentLog.getOperateType())) {//开启
                        ctrlEquipment.setIsService("1");
                    } else {    //关闭
                        ctrlEquipment.setIsService("0");
                    }
                    ctrlequipmentDao.updateSelective(ctrlEquipment);

                    return Result.success();
                }
                //大于10秒后执行不成功则表示失败
                if (sleepTime >= 10000 && "1".equals(ctrlEquipmentLog1.getDelFlag())) {
                    return Result.fail().add("设备已成功接收到指令，但未响应");
                }
            }
        } else {
            return Result.fail().add("出现未知错误，请重试");
        }
    }

    /**
     * 删除控制设备操作记录
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteCtrlEquipmentLog(String id) {
        ctrlequipmentLogDao.deleteById(id);
        return Result.success();

    }

    /**
     * 修改控制设备操作记录
     *
     * @param ctrlEquipmentLog
     * @return Result
     */
    @Override
    public Result editCtrlEquipmentLog(CtrlequipmentLog ctrlEquipmentLog) {

        CtrlequipmentLog oldCtrlequipmentLog =ctrlequipmentLogDao.findById(ctrlEquipmentLog.getId()).get();
        BeanUtils.copyProperties(ctrlEquipmentLog,oldCtrlequipmentLog, NullAttributeUtils.getNullPropertyNames(ctrlEquipmentLog));



        ctrlequipmentLogDao.save(oldCtrlequipmentLog);
        return Result.success();

    }

    /**
     * 查询控制设备操作记录详细信息
     *
     * @param id
     * @return CtrlequipmentLog
     */
    @Override
    public CtrlequipmentLog getCtrlEquipmentLogById(String id) {




        return ctrlequipmentLogDao.findById(id).get();
    }

    /**
     * 查询控制设备操作记录详细信息列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentLogList() {
        List<CtrlequipmentLog> ctrlEquipmentLogList = ctrlequipmentLogDao.findAll();
        return Result.success().add(ctrlEquipmentLogList);
    }

    /**
     * 分页查询控制设备操作记录详细信息列表
     *
     * @param page
     * @param rows
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentLogPage(int page, int rows) {
        PageRequest pageRequest = PageRequest.of(page - 1, rows, Sort.Direction.DESC, "createDate");

        Page<CtrlequipmentLog> p = ctrlequipmentLogDao.findAll(pageRequest);

        Result result = new Result();
        result.setCode(StatusCode.SUCCESS);
        result.setData(p.getContent());
        return result;
    }
}
